#include "TimeLine.hpp"
#include "AbstractEvent.hpp"
#include <QxtLogger>
TimeLine::TimeLine()
{
    this->timebase = 0.0f;
    started = false;
}

void TimeLine::insert(AbstractEvent *event) {
    qxtLog->trace("Inserting an event: "+event->name()+" :: "+QByteArray::number(event->time()));
    data.insert(event->time(),event);
    if(!typeCount.contains(event->name())) {
        typeCount.insert(event->name(),1);
    } else {
        typeCount[event->name()]+=1;
    }
}

int TimeLine::eventCount() const {
    return data.size();
}

void TimeLine::update(float delta) {
    if(started && eventCount()) {
        timebase += delta;
        while(eventCount() && data.begin().key() <= timebase) {
            data.begin().value()->dispatch();
            data.remove(data.begin().key());
        }
    }
    if(!eventCount())
        timebase = 0.0f;
}

void TimeLine::start() {
    started = true;
}
